//
//  YZChatMessageImageCell.m
//  CocoaAsyncSocketClient
//
//  Created by lixiaoyi on 2018/6/26.
//  Copyright © 2018年 lixiaoyi. All rights reserved.
//

#import "YZChatMessageImageCell.h"
#import "YZMessageHelper.h"
#import "YZFileTool.h"
#import "YZMessage.h"
#import "YZMessageModel.h"
#import "YZMessageConst.h"
#import "YZMediaManager.h"

@interface YZChatMessageImageCell()

@property (nonatomic, strong) UIButton *imageBtn;

@end

@implementation YZChatMessageImageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.imageBtn];
    }
    
    return self;
}

#pragma mark - lazy
- (UIButton *)imageBtn {
    if (!_imageBtn) {
        _imageBtn = [UIButton new];
        [_imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        _imageBtn.imageEdgeInsets = UIEdgeInsetsZero;
        _imageBtn.layer.cornerRadius = 5;
        _imageBtn.layer.masksToBounds = YES;
        _imageBtn.clipsToBounds = YES;
    }
    
    return _imageBtn;
}


#pragma mark - event
- (void)imageBtnClick:(UIButton *)btn {
    if (!btn.currentBackgroundImage) {
        return;
    }
    
    CGRect smallRect = [YZMessageHelper photoFrameInWindow:btn];
    CGRect bigRect = [YZMessageHelper photoLargerInWindow:btn];
    NSValue *smallValue = [NSValue valueWithCGRect:smallRect];
    NSValue *bigValue = [NSValue valueWithCGRect:bigRect];
    [self routerEventWithName:GXRouterEventImageTapEventName
                     userInfo:@{MessageKey   : self.modelFrame,
                                @"smallRect" : smallValue,
                                @"bigRect"   : bigValue
                                }];
    
}

#pragma mark - private method
- (void)setModelFrame:(YZMessageFrame *)modelFrame {
    [super setModelFrame:modelFrame];
    YZMediaManager *manager = [YZMediaManager shareManager];
    UIImage *image = [manager imageWithLocalPath:[manager imagePathWithName:modelFrame.model.mediaPath.lastPathComponent]];
//    UIImage *image = [manager imageWithLocalPath:modelFrame.model.mediaPath];
    self.imageBtn.frame = modelFrame.picViewF;
    self.bubbleView.userInteractionEnabled = _imageBtn.imageView.image != nil;
    self.bubbleView.image = nil;
    if (modelFrame.model.isSender) {
        UIImage *arrowImage = [manager arrowMeImage:image size:modelFrame.picViewF.size mediaPath:modelFrame.model.mediaPath isSender:modelFrame.model.isSender];
        [self.imageBtn setBackgroundImage:arrowImage forState:UIControlStateNormal];
    } else {
        NSString *orgImgPath = [manager originImgPath1:modelFrame];
        if ([YZFileTool fileExistsAtPath:orgImgPath]) {
            UIImage *orgImg = [manager imageWithLocalPath:orgImgPath];
            UIImage *arrowImage = [manager arrowMeImage:orgImg size:modelFrame.picViewF.size mediaPath:orgImgPath isSender:modelFrame.model.isSender];
            [self.imageBtn setBackgroundImage:arrowImage forState:UIControlStateNormal];
        } else {
//            UIImage *arrowImage = [manager arrowMeImage:image size:modelFrame.picViewF.size mediaPath:modelFrame.model.mediaPath isSender:modelFrame.model.isSender];
            [self.imageBtn setBackgroundImage:image forState:UIControlStateNormal];
        }
    }
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
